<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="tong.li&#39;s blog">
  <meta name="keyword" content="彤哥哥博客，95后技术爱好者,现就职于同程旅行/同程艺龙上海分公司，专注于互联网技术分享的平台。">
  
    <link rel="shortcut icon" href="/css/images/icon.png">
  
  <title>
    
      Linux基础-SSH免密登录 | 彤哥哥的博客
    
  </title>
  <link href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/highlight.js/9.12.0/styles/tomorrow-night.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdn.staticfile.org/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>彤哥哥的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/series/" class="item-link">分类</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">项目</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/series/" class="menu-link">分类</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">项目</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>Linux基础-SSH免密登录</h2>
  <p class="post-date">2019-11-23</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><p>Linux SSH登录由两种方式:</p>
<ol>
<li><p>账户密码登录</p>
</li>
<li><p>密钥登录</p>
</li>
</ol>
<p>我有两台主机，ip分别为192.168.5.1和192.168.5.100，我要从前者远程登录到后者主机上，下面我分别来演示一下两种方式:</p>
<h1 id="1-账号密码登录"><a href="#1-账号密码登录" class="headerlink" title="1. 账号密码登录"></a>1. 账号密码登录</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh root@192.168.5.100</span><br></pre></td></tr></table></figure>

<p>注意： 默认端口号是22,如果不是22需要加入-p+端口号进行登录，比如远程登录示例:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ ssh -p22 root@192.168.5.100</span><br><span class="line">root@192.168.5.100<span class="string">&#x27;s password: </span></span><br><span class="line"><span class="string">Last login: Sat Nov 23 18:34:53 2019 from 192.168.5.1</span></span><br><span class="line"><span class="string">[root@localhost ~]# ifconfig ens33</span></span><br><span class="line"><span class="string">ens33: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500</span></span><br><span class="line"><span class="string">        inet 192.168.5.100  netmask 255.255.255.0  broadcast 192.168.5.255</span></span><br><span class="line"><span class="string">        inet6 fe80::79ad:6fa6:c473:3647  prefixlen 64  scopeid 0x20&lt;link&gt;</span></span><br><span class="line"><span class="string">        ether 00:0c:29:b3:a4:0f  txqueuelen 1000  (Ethernet)</span></span><br><span class="line"><span class="string">        RX packets 281  bytes 40054 (39.1 KiB)</span></span><br><span class="line"><span class="string">        RX errors 0  dropped 0  overruns 0  frame 0</span></span><br><span class="line"><span class="string">        TX packets 211  bytes 37007 (36.1 KiB)</span></span><br><span class="line"><span class="string">        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0</span></span><br></pre></td></tr></table></figure>

<h1 id="2-公钥免密登录"><a href="#2-公钥免密登录" class="headerlink" title="2. 公钥免密登录"></a>2. 公钥免密登录</h1><ol>
<li><p>先在192.168.5.1这太机器上生成公钥；</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen -t rsa</span><br></pre></td></tr></table></figure>

<p>执行上述命令后，敲三次回车即可生成，生成后会在当前账号的home的.ssh目录中。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ ssh-keygen -t rsa</span><br><span class="line">Generating public/private rsa key pair.</span><br><span class="line">Enter file <span class="keyword">in</span> <span class="built_in">which</span> to save the key (/home/litong/.ssh/id_rsa): </span><br><span class="line">/home/litong/.ssh/id_rsa already exists.</span><br><span class="line">Overwrite (y/n)? </span><br><span class="line">litong@LT:~$ <span class="built_in">ls</span> -l ~/.ssh/</span><br><span class="line">总用量 12</span><br><span class="line">-rw------- 1 litong litong 1675 9月  10 22:03 id_rsa</span><br><span class="line">-rw-r--r-- 1 litong litong  400 9月  10 22:03 id_rsa.pub</span><br><span class="line">-rw-r--r-- 1 litong litong 2214 11月 20 21:54 known_hosts</span><br></pre></td></tr></table></figure>

<p>​       authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥,这个文件可以没有；</p>
<p>　　id_rsa : 生成的私钥文件；</p>
<p>　　id_rsa.pub ： 生成的公钥文件；</p>
<p>　　know_hosts : 已知的主机公钥清单。</p>
<p>注意:</p>
<p>​     1).  <strong>ssh目录的权限必须是700</strong> </p>
<p>​     2). <strong>.ssh/authorized_keys文件权限必须是600</strong></p>
</li>
<li><p> 确认公钥文件id_rsa.pub是否存在，并把公钥文件上传到远程登录的机器上。</p>
</li>
</ol>
<p>   复制文件有多种方式：</p>
<ol>
<li><p>通过ssh-copy-id的方式</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.5.100</span><br></pre></td></tr></table></figure></li>
<li><p>通过scp将内容写到对方的文件中</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scp -p ~/.ssh/id_rsa.pub root@192.168.5.100:/root/.ssh/authorized_keys</span><br></pre></td></tr></table></figure></li>
<li><p>上传成功后，查看远程主机~/.ssh/authorized_keys文件的内容，其中内容包含要远程主机的公钥则说吗配置完成。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~/.ssh$ <span class="built_in">cat</span> id_rsa.pub  <span class="comment"># 192.168.5.1机器公钥</span></span><br><span class="line">   ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEeCQP2arzLybGinLvKEgN6cJtINUhDL8uLIwaCJdsv7FbYrBK9R2I01F+pftjiiILCrg0GgwSThAJ7cCxbmriw+D1ZoZdC+MJPONiv4PF9KBWNTGdZ1fLjpEifUI3Aax23TdQqkHs+4+T8zOK2v4MsDgit4nqJheV2I+cXuumPlCONndfr43KxPsGJqAtb8yzkMA20/r8fnnOMbfblDnsAOTGWjC+ond2Eph5AlY2tW+s9JiFBHVawalheqYBVmKlNT3vqVp5IKrnJrIVagN59FKqSOAYTFc/7eR61npGtf0+Ftew137FICGp7F6v4JxjqNuSkxsXsH6+2Z071A3X ltyeamin@gmail.com</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">root@localhost .ssh]<span class="comment"># cat authorized_keys # 192.168.5.100远程机器免密登录公钥</span></span><br><span class="line">   ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEeCQP2arzLybGinLvKEgN6cJtINUhDL8uLIwaCJdsv7FbYrBK9R2I01F+pftjiiILCrg0GgwSThAJ7cCxbmriw+D1ZoZdC+MJPONiv4PF9KBWNTGdZ1fLjpEifUI3Aax23TdQqkHs+4+T8zOK2v4MsDgit4nqJheV2I+cXuumPlCONndfr43KxPsGJqAtb8yzkMA20/r8fnnOMbfblDnsAOTGWjC+ond2Eph5AlY2tW+s9JiFBHVawalheqYBVmKlNT3vqVp5IKrnJrIVagN59FKqSOAYTFc/7eR61npGtf0+Ftew137FICGp7F6v4JxjqNuSkxsXsH6+2Z071A3X ltyeamin@gmail.com</span><br></pre></td></tr></table></figure>

</li>
<li><p>尝试免密登录。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~/.ssh$ ssh root@192.168.5.100</span><br><span class="line">Last login: Sat Nov 23 19:07:01 2019 from 192.168.5.1</span><br></pre></td></tr></table></figure></li>
</ol>
<h1 id="3-总结"><a href="#3-总结" class="headerlink" title="3. 总结"></a>3. 总结</h1><p>  日常工作中，免密登录用处特别多，项目部署的时候上传时候需要免密登录，不可能将密码原文设置到配置文件中，这样特别不安全。</p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Linux" >
    <span class="tag-code">Linux</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2019/11/20/RocketMQ%E6%B6%88%E6%81%AF%E5%8F%91%E9%80%81%E6%A0%B7%E4%BE%8B/">
        <span class="nav-arrow">← </span>
        
          RocketMQ消息发送样例
        
      </a>
    
    
      <a class="nav-right" href="/2019/11/27/Zookeeper%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/">
        
          Zookeeper集群搭建
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Utterances START -->
      <div id="utterances"></div>
      <script src="https://utteranc.es/client.js"
        repo="ltyeamin/blogtalks"
        issue-term="pathname"
        theme="github-light"
        crossorigin="anonymous"
        async></script>    
      <!-- Utterances END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#1-%E8%B4%A6%E5%8F%B7%E5%AF%86%E7%A0%81%E7%99%BB%E5%BD%95"><span class="toc-nav-text">1. 账号密码登录</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#2-%E5%85%AC%E9%92%A5%E5%85%8D%E5%AF%86%E7%99%BB%E5%BD%95"><span class="toc-nav-text">2. 公钥免密登录</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#3-%E6%80%BB%E7%BB%93"><span class="toc-nav-text">3. 总结</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://example.com/2019/11/23/Linux基础-SSH免密登录/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2024 | Proudly powered by <a href="https://hexo.io" target="_blank">Hexo</a>
    <br>
    Theme by <a target="_blank" rel="noopener" href="https://github.com/ltyeamin">tong.li</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("https://cdn.staticfile.org/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("https://cdn.staticfile.org/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->



<script src='https://cdn.staticfile.org/mermaid/8.11.2/mermaid.min.js'></script>



<script src="/js/script.js"></script>


  </body>
</html>